package com.leyantech.ecp.agenx.common.di;

import com.leyantech.chaos.kafka.KafkaProducerStringClient;
import com.leyantech.ecp.agenx.common.sender.Dispatcher;
import com.leyantech.ecp.agenx.common.sender.Sender;

import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-12-01.
 */
public class MessageDispatcherModule extends AbstractModule {

  private final boolean needProvideProducer;

  public MessageDispatcherModule() {
    this.needProvideProducer = true;
  }

  public MessageDispatcherModule(boolean needProvideProducer) {
    this.needProvideProducer = needProvideProducer;
  }

  @Override
  protected void configure() {

    bind(new TypeLiteral<Sender<String>>() {
    })
        .annotatedWith(Names.named("dispatcher"))
        .to(Dispatcher.class)
        .asEagerSingleton();

    if (needProvideProducer) {
      bind(KafkaProducerStringClient.class)
          .toInstance(KafkaProducerStringClient.newBuilder("dispatcher").build());
    }

  }


}
